﻿using System;
using WorkOrder.Model;
namespace WorkOrder.Business
{
    public class Business_Table_Exception
    {
        DbHelper dbhelper = new DbHelper();

        #region 保存
        public bool Insert(Model_Table_Exception model)
        {
            string sqlstr = " insert into Table_Exception (exception_id,exception_type_id,order_id,exception,create_time,creator) values(";
            sqlstr += "NEWID(),"+model.Exception_type_id+",'"+model.Order_id+"','" + model.Exception + "','" + DateTime.Now.ToString() + "',"+model.Creator+")";
            if (dbhelper.ExecuteNoQuery(sqlstr) > 0)
                return true;
            else
                return false;
        }
        #endregion

        #region 更新
        public bool Update(Model_Table_Exception model)
        {
            string sql = "update Table_Exception set exception_type_id=" + model.Exception_type_id
                + ", exception='" + model.Exception + "' where exception_id='" + model.Exception_id + "'";
            if (dbhelper.ExecuteNoQuery(sql) > 0)
                return true;
            else
                return false;
        }
        #endregion

        #region 得到单个数据集
        public Model_Table_Exception GetData(string keyValue)
        {
            string sql = "select * from Table_Exception where exception_id=" + keyValue;
            using (System.Data.SqlClient.SqlDataReader rdr = dbhelper.Search(sql))
            {
                if (rdr != null && rdr.Read())
                {
                    Model_Table_Exception tmp;
                    tmp = new Model_Table_Exception(rdr["exception_id"].ToString(), rdr["exception_type_id"].ToString(), rdr["order_id"].ToString(),
                        rdr["exception"].ToString(), Convert.ToDateTime(rdr["create_time"]), rdr["creator"].ToString());
                    rdr.Close();
                    dbhelper.CloseDbConnection();
                    return tmp;
                }
                else return null;
            }
        }
        #endregion

        #region 根据条件得到数据集
        public System.Data.DataSet GetDatas(string condition, int pageSize, int currentPage, string orderField, OrderType ot, string userID)
        {
            string sql = "UP_GET_PAGEINFO 'Table_Exception','*"
                     + "','" + condition + "','','" + orderField + "'," + (ot == OrderType.ASC ? 0 : 1) + "," + pageSize + "," + currentPage;
            return dbhelper.Query(sql);
        }
        
        #endregion

        #region 删除记录
        public bool Delete(string fieldName, string fieldValue)
        {
            string sqlstr = "delete Table_Exception where " + fieldName + "=" + fieldValue;
            if (dbhelper.ExecuteNoQuery(sqlstr) > 0)
                return true;
            else
                return false;
        }
        #endregion

        public System.Data.DataSet GetDatas(string order_id)
        {
            string sqlstr = "select * from Table_Exception where order_id='" + order_id + "'";
            return dbhelper.Query(sqlstr);
        }

        #region 返回所有工单问题类型
        public System.Data.DataSet GetExceptType()
        {
            string sqlstr = "select exception_type_id,exception_type from Table_Exception_type";
            return dbhelper.Query(sqlstr);
        }
        public System.Data.DataSet GetExceptType(string condition)
        {
            string sql = "select * from Table_Exception_type where " + condition;
            return dbhelper.Query(sql);
        }
        #endregion
    }
}
